package demo5;

import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.SynchronousQueue;

/**
 * @Author 12629
 * @Description：
 */
class Student implements Comparable<Student>{
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age-o.age;
    }
}
//比较器
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age-o2.age;
    }
}

class ScoreComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score-o2.score);
    }
}

class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

public class Test {

    public static void sort(Comparable[] array) {
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j].compareTo(array[j+1]) > 0) {
                    // 说明顺序不符合要求, 交换两个变量的位置
                    Comparable tmp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("bit",10,78.9);
        students[1] =  new Student("abc",4,98.9);
        students[2] =  new Student("gb",14,38.9);
        System.out.println("排序前"+Arrays.toString(students));
        sort(students);
        System.out.println("排序后"+Arrays.toString(students));
    }

    public static void main8(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("bit",10,78.9);
        students[1] =  new Student("abc",4,98.9);
        students[2] =  new Student("gb",14,38.9);
        System.out.println("排序前"+Arrays.toString(students));

        AgeComparator ageComparator = new AgeComparator();
        ScoreComparator scoreComparator = new ScoreComparator();
        NameComparator nameComparator = new NameComparator();

        Arrays.sort(students,nameComparator);

        System.out.println("排序后"+Arrays.toString(students));
    }

    public static void main3(String[] args) {
        Student student1 = new Student("bit",10,78.9);
        Student student2 = new Student("abc",4,98.9);

        AgeComparator ageComparator = new AgeComparator();
        int ret = ageComparator.compare(student2,student1);
        System.out.println(ret);

        ScoreComparator scoreComparator = new ScoreComparator();
        int ret2 = scoreComparator.compare(student1,student2);
        System.out.println("分数比较："+ret2);

    }

    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("bit",10,78.9);
        students[1] =  new Student("abc",4,98.9);
        students[2] =  new Student("gb",14,38.9);
        System.out.println("排序前"+Arrays.toString(students));
        Arrays.sort(students);
        System.out.println("排序后"+Arrays.toString(students));
    }



    public static void main1(String[] args) {
        Student student1 = new Student("bit",10,78.9);
        Student student2 = new Student("abc",4,98.9);
        /*if(student1.compareTo(student2) > 0 ) {
            System.out.println("student1>student2");
        }*/
        //比较这两个对象的大小
       /* int a = 10;
        int b = 20;
        if(a > b) {

        }*/
    }
}
